package views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.zx.theline.R;

import java.util.ArrayList;
import java.util.List;

import GsonBean.JcbAqfxListBean;
import app.MyApplication;
import greendao.entity.Safety;
import me.xiaopan.android.widget.LinearLineWrapLayout;

/**
 * Created by Ren on 2017/6/20.
 * 风险评价
 */

public class RiskEvaluationView extends LinearLayout {

    //风险列表
    List<JcbAqfxListBean> dataList;

    List<View> itemList;

    //施工安全字典表
    List<Safety> safetyList;

    //是否只读 true为只读 无法评价 显示已经存在评价内容   false为不是只读，允许修改评价内容
    boolean onlyRead;

    OnItemClickListen itemClickListen;


    //总体评价
    String ztpj;


    //是否有问题
    boolean hasProblem = false;


    public RiskEvaluationView(Context context, List<JcbAqfxListBean> dataList, boolean onlyRead, OnItemClickListen itemClickListen) {
        super(context);
        this.dataList = dataList;
        this.onlyRead = onlyRead;
        this.itemClickListen = itemClickListen;
        setView();
    }


    public RiskEvaluationView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RiskEvaluationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void setView() {

        itemList = new ArrayList<>();

        //设置竖直方向排列
        setOrientation(VERTICAL);

        setPadding(5, 5, 10, 5);

        //从本地数据库获取安全问题list
        safetyList = MyApplication.getDaoSession().getSafetyDao().loadAll();


        //如果没有风险内容
        if (dataList.size() == 0) {
            return;
        } else {
            for (int index=0;index<dataList.size();index++) {
                JcbAqfxListBean mBean=dataList.get(index);
                //风险评价中 每一个风险点
                View fxpjItem;

                if (onlyRead) {
                    fxpjItem = inflate(getContext(), R.layout.fxlb_chakan, null);
                    TextView aqfxnr = (TextView) fxpjItem.findViewById(R.id.aqfxnr);
                    TextView fxpj = (TextView) fxpjItem.findViewById(R.id.fxpj);
                    aqfxnr.setText(mBean.getAqfxnr());
                    fxpj.setText(mBean.getAqfxpj());
                    addView(fxpjItem);
                    itemList.add(fxpjItem);
                } else {

                    fxpjItem = inflate(getContext(), R.layout.fxlb, null);
                    final LinearLineWrapLayout aqwt_layout = (LinearLineWrapLayout) fxpjItem.findViewById(R.id.aqwt_layout);
                    TextView aqfxnr = (TextView) fxpjItem.findViewById(R.id.aqfxnr);
                    aqfxnr.setText(mBean.getAqfxnr());

                    //选择评价spinner
                    final Spinner Evaluation = (Spinner) fxpjItem.findViewById(R.id.fxpj);
                    Evaluation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            //根据整体评价设置safetymodule
                            setsafetymodule(Evaluation.getSelectedItem().toString(), aqwt_layout);
                            ztpj = UpdataZtpj();
                            itemClickListen.itemClickListen(ztpj);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });
                    //根据安全风险评价，设置spinner
                    if(mBean.getAqfxpj()==null){
                        Evaluation.setSelection(0);
                    } else {
                        Evaluation.setSelection(Integer.parseInt(mBean.getAqfxpj())-1);
                    }


                    if (safetyList.size() != 0) {
                        for (int i = 0; i < safetyList.size(); i++) {
                            Safety enetity = safetyList.get(i);
                            CheckBox mCheckBox = (CheckBox) LayoutInflater.from(getContext()).inflate(R.layout.checkbox_safety, null);
                            mCheckBox.setText(enetity.getSgwtmc());
                            /**
                             * 设置安全问题checkbox是否选中
                             * */
                            //sgwtflid格式 "1,2,5"
                            String sgwtfl=dataList.get(index).getSgwtflId();
                            if(sgwtfl!=null) {
                                String[] sgwtflList = sgwtfl.split(",");
                                //循环检测 对存在的施工问题编号进行选中
                                for (int j = 0; j < sgwtflList.length; j++) {
                                    if (enetity.getSgwtflId().equals(sgwtflList[j])) {
                                        mCheckBox.setChecked(true);
                                    }
                                }
                            }
                            LinearLineWrapLayout.LayoutParams lp = new LinearLineWrapLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            lp.setMargins(5, 5, 5, 0);
                            mCheckBox.setLayoutParams(lp);
                            aqwt_layout.addView(mCheckBox);
                        }
                    } else {
                        TextView tv=new TextView(getContext());
                        tv.setText("没有安全问题，请到用户中心更新数据");
                        aqwt_layout.addView(tv);
                    }

                    addView(fxpjItem);
                    itemList.add(fxpjItem);
                }
            }
        }

    }

    //根据安全风险评价文字 获取对应的spinner position
    public int getSpinnerSelectIdByString(String aqfxpj) {
        if(aqfxpj==null)
            return 1;
        if(aqfxpj.equals("好"))
            return 1;
        if(aqfxpj.equals("正常"))
            return 2;
        if(aqfxpj.equals("基本正常"))
            return 3;
        if(aqfxpj.equals("有问题"))
            return 4;
        else return 1;
    }



    //每一个风险点选择评价以后 根据每一个风险点评价情况 更改总体评价情况
    private String UpdataZtpj() {

        //        评价级别 好：4 正常：3 基本正常：2 有问题：1
        int level = 4;

        if (itemList.size() != 0) {
            for (int position = 0; position < getChildCount(); position++) {
                int curLevel;
                View fxpjItem = itemList.get(position);
                String fxpj = ((Spinner) fxpjItem.findViewById(R.id.fxpj)).getSelectedItem().toString();
                if (fxpj.equals("有问题")) {
                    level = 1;
                }
                if (fxpj.equals("好")) {
                    curLevel = 4;
                    if (curLevel < level) {
                        level = curLevel;
                    }
                }
                if (fxpj.equals("正常")) {
                    curLevel = 3;
                    if (curLevel < level) {
                        level = curLevel;
                    }
                }
                if (fxpj.equals("基本正常")) {
                    curLevel = 2;
                    if (curLevel < level) {
                        level = curLevel;
                    }
                }
            }
        }

        if (level == 4) {
            return "好";
        }

        if (level == 3) {
            return "正常";
        }

        if (level == 2) {
            return "基本正常";
        }

        if (level == 1) {
            return "有问题";
        }

        return "好";
    }


    //设置安全问题模块 是否显示
    private void setsafetymodule(String selectPJ, LinearLineWrapLayout aqwt_layout) {

        if (selectPJ.equals("有问题")) {
            aqwt_layout.setVisibility(View.VISIBLE);
        } else {
            aqwt_layout.setVisibility(View.GONE);
        }
    }

    //获取所有风险点点view
    public List<View> getItemList() {
        return itemList;
    }

    //根据position获取对应 itemview下面的

    public String getSafetyById(int positon) {
        if (positon < itemList.size()) {
            String safety = "";
            LinearLineWrapLayout aqwt_layout = (LinearLineWrapLayout) itemList.get(positon).findViewById(R.id.aqwt_layout);
            //如果已经选中施工安全问题，但是又从有问题改成其他，aqwt_layout隐藏，根据aqwt_layout是否显示 判断 返回，aqwt_layout gone return ""
            if (aqwt_layout.getVisibility() != GONE) {
                for (int i = 0; i < aqwt_layout.getChildCount(); i++) {
                    CheckBox mCheck = (CheckBox) aqwt_layout.getChildAt(i);
                    if (mCheck.isChecked()) {
                        safety += safetyList.get(i).getSgwtflId() + ",";
                    }
                }
                if (safety.length() != 0)
                    safety = safety.substring(0, safety.length() - 1);
            }
            return safety;
        } else {
            throw new IllegalArgumentException("参数值超过List内部对象个数");
        }

    }

    public interface OnItemClickListen {
        void itemClickListen(String ZTPJ);
    }

}
